package com.neouton.crm.executiceManager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.common.util.ResponseAPI;
import com.neouton.crm.executiceManager.entity.ManagerContractInfoVO;
import com.neouton.crm.executiceManager.service.IManagerContractService;
import com.neouton.crm.pm.entity.ContractInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("manager")
public class ManagerContractController {

    @Autowired
    private IManagerContractService managerContractService;

    /**
     * 分页查新合同信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryContractsByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryContractsByPage(@PathVariable(value = "pageNum") int pageNum,
                                            @PathVariable(value = "pageSize") int pageSize,
                                            @RequestBody ManagerContractInfoVO entity,
                                            @RequestHeader("token") String token) {
        IPage<ManagerContractInfoVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerContractInfoVO> result = managerContractService.queryContractsByPage(page ,entity ,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 保存合同信息
     * @param contractInfoVO
     * @return
     */
    @PostMapping("/saveOrUpdateContractInfo")
    public ResponseAPI saveOrUpdateContractInfo(@RequestHeader("token") String token,@RequestBody ManagerContractInfoVO contractInfoVO){
        contractInfoVO.setCreator(AuthUtils.getUserId());
        contractInfoVO.setLastUpdateBy(AuthUtils.getUserId());
        Integer contractId = managerContractService.saveOrUpdateContractInfo(contractInfoVO,token);
        return ResponseAPI.successData(contractId);
    }

    /**
     * 通过审批ID获取合同信息
     * @param approvalId
     * @return
     */
    @PostMapping("/getContractInfoByApprovalId/{approvalId}")
    public ResponseAPI getContractInfoByApprovalId(@PathVariable(value = "approvalId") Integer approvalId){
        ManagerContractInfoVO contractInfoVO = managerContractService.queryContractInfoByapprovalId(approvalId);
        return ResponseAPI.successData(contractInfoVO);
    }

    /**
     * 通过审批ID获取历史合同信息
     * @param approvalId
     * @return
     */
    @PostMapping("/getContractInfoHistoryByApprovalId/{approvalId}")
    public ResponseAPI getContractInfoHistoryByApprovalId(@PathVariable(value = "approvalId") Integer approvalId){
        ManagerContractInfoVO contractInfoVO = managerContractService.getContractInfoHistoryByApprovalId(approvalId);
        return ResponseAPI.successData(contractInfoVO);
    }

    /**
     * 通过合同ID获取合同信息
     * @param contractId
     * @return
     */
    @PostMapping("/getContractInfoById/{contractId}")
    public ResponseAPI getContractInfoById(@PathVariable(value = "contractId") int contractId){
        ManagerContractInfoVO contractInfoVO = managerContractService.queryContractInfoById(contractId);
        return ResponseAPI.successData(contractInfoVO);
    }

    /**
     * 合同信息驳回审批
     * @param contractInfoVO 合同参数
     * @return
     */
    @PostMapping("rejectionContractInfo")
    public ResponseAPI rejectionContractInfo(@RequestBody ManagerContractInfoVO contractInfoVO,@RequestHeader("token") String token) {
        Boolean flage = managerContractService.rejectionContractInfo(contractInfoVO,token);
        return ResponseAPI.successData(flage);
    }

    /**
     * 合同信息通过审批
     * @param contractInfoVO 合同参数
     * @return
     */
    @PostMapping("approveContractInfo")
    public ResponseAPI approveContractInfo(@RequestBody ManagerContractInfoVO contractInfoVO,@RequestHeader("token") String token) {
        Boolean flage = managerContractService.approveContractInfo(contractInfoVO,token,false);
        return ResponseAPI.successData(flage);
    }

    @PostMapping("/exportContractInfo")
    public void contractInfoExport(HttpServletResponse response,
                                   @RequestBody ManagerContractInfoVO entit,
                                   @RequestHeader("token") String token) throws Exception{
        managerContractService.exportContractInfo(response,entit,token);
    }
    /**
     * 删除合同单
     * @param purchaseOrdeId
     * @return
     */
    @PostMapping("/deleteContractInfoById/{contractId}")
    public ResponseAPI deleteContractInfoById(@PathVariable(value = "contractId") int contractId){
        return ResponseAPI.successData(managerContractService.deleteContractInfoById(contractId));
    }


    /**
     * 分页查询合同待办信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalContractByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalContractInfoByPage( @PathVariable(value = "pageNum") int pageNum,
                                                        @PathVariable(value = "pageSize") int pageSize,
                                                        @RequestBody ManagerContractInfoVO entity) {
        IPage<ManagerContractInfoVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerContractInfoVO> result = managerContractService.queryApprovalContractByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 分页查询合同已办信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalContractInfoDoneByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalContractInfoDoneByPage( @PathVariable(value = "pageNum") int pageNum,
                                                            @PathVariable(value = "pageSize") int pageSize,
                                                            @RequestBody ManagerContractInfoVO entity) {
        IPage<ManagerContractInfoVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerContractInfoVO> result = managerContractService.queryApprovalContractDoneByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 分页查询合同申请记录信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalContractInfoRecordByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalContractInfoRecordByPage( @PathVariable(value = "pageNum") int pageNum,
                                                              @PathVariable(value = "pageSize") int pageSize,
                                                              @RequestBody ManagerContractInfoVO entity,
                                                              @RequestHeader("token") String token) {
        IPage<ManagerContractInfoVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerContractInfoVO> result = managerContractService.queryApprovalContractRecordByPage(page ,entity,token);
        return ResponseAPI.successData(result);
    }
}
